import java.{io};
import java.{util};
import java.{awt};
import javax.swing.{BoxLayout, KeyStroke, filechooser, DefaultComboBoxModel};
import scala.{swing}
import scala.collection.mutable.{ Buffer, ArrayBuffer };
import scala.util.matching.{Regex};

import ul.iobj.{IAttr, IObj};
import ul.gettext.{GetText};
import ul.{audio};
import ul.elab._;


object ELab extends swing.SimpleSwingApplication {

    def top = new swing.MainFrame {

        loadConfig; // load configuration
        loadLocale; // load localization
        val tr: (String)=>String = Config.tran.tr _;
      
        // build GUI
        title = Config.projectName;

        val mainPanel = new swing.TabbedPane {
        

            val rlcPanel = new swing.GridBagPanel {
            };

            val afcPanel = new swing.GridBagPanel {
            };

            val oscPanel = new swing.GridBagPanel {
            };

            GeneratorPanel.tabs = this;
            pages ++= List(
                new swing.TabbedPane.Page( tr("Configuration"), ConfigPanel, tr("Program configuration") )
                ,new swing.TabbedPane.Page( tr("Generator"), GeneratorPanel, tr("Waveform generator") )
                ,new swing.TabbedPane.Page( tr("RLC"), rlcPanel, tr("RLC meter") )
                ,new swing.TabbedPane.Page( tr("AFC"), afcPanel, tr("AFC meter") )
                ,new swing.TabbedPane.Page( tr("Scope"), afcPanel, tr("Oscilloscope") )
            );
            selection.index = Config("tabIndex").i;
            
        }
        contents = mainPanel;

        // restore window geometry
        minimumSize = new awt.Dimension( 400, 300 );
        bounds = new awt.Rectangle( Config("winX").i, Config("winY").i, Config("winW").i, Config("winH").i );
        
        override def closeOperation() {
            saveConfig;
            super.closeOperation()
        }
        
        def loadConfig = {
            try {
                val configIn = new io.FileInputStream(Config.configFile)
                Config.attrsFromConf( new String((for (i <- 1 to configIn.available()) yield configIn.read().toByte).toArray, "utf8") )
                configIn.close()
            } catch { case _ => }
        }
        
        def loadLocale = {
            Config.tran = new GetText("./locale", Config.projectName)
            Config.tran.lang = Config("lang").s
            Config.tr = Config.tran.tr _
            val loc = new util.Locale( Config.tran.lang )
            util.Locale.setDefault(loc)
        }

        def saveConfig = {
        
            // update configuration
            Config("winX").i = bounds.x; Config("winY").i = bounds.y;
            Config("winW").i = bounds.width; Config("winH").i = bounds.height;
            Config("tabIndex").i = mainPanel.selection.index;
            Config("sourceMixer").s = ConfigPanel.sourceMixerComboBox.selection.item.toString;
            Config("sourceLine").s =
                if (ConfigPanel.sourceLineComboBox.selection.item == null) ""
                else ConfigPanel.sourceLineComboBox.selection.item.toString;
            Config("lang").s = Config.tran.langs( ConfigPanel.langComboBox.selection.index );

            val configOut = new io.FileOutputStream(Config.configFile);
            configOut.write( Config.attrsToConf.getBytes( "utf8" ) );
            configOut.close();
        }
        
    }

}

